#include "enemy_2.h"

extern int G_W, G_H;
extern QPoint shuttle_pos;

ENEMY_2::ENEMY_2(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_2/go.ogg",this);
    go->Play_Now();

    qsrand(QTime::currentTime().second());

    left_orientation= 0+qrand()%(1-0+1);    // Появить с лева или с права на экране

    if(left_orientation){
        stvol_1=QPoint(30,17);
        stvol_2=QPoint(27,49);
        stvol_3=QPoint(24,186);
        stvol_4=QPoint(21,218);
    }
    else{
        stvol_1=QPoint(48,18);
        stvol_2=QPoint(52,49);
        stvol_3=QPoint(56,185);
        stvol_4=QPoint(56,222);
    }

    point=30;

    if(left_orientation){
        border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_2/Enemy_2_left.poly");
        im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_2/Enemy_2_left.png");
    }
    else{
        border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_2/Enemy_2_right.poly");
        im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_2/Enemy_2_right.png");
    }

    this->setZValue(0.5);

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line = new QTimeLine(7000);                                             // Скорость перемещения обьекта по траектории
    enemy_time_line->setEasingCurve( QEasingCurve::Type(25));

    enemy_time_line->setFrameRange(0, 1);
    anim_enemy->setTimeLine(enemy_time_line);
    anim_enemy->setItem(this);

    if(left_orientation){
        this->setPos( QPointF (0,im->height()*(-1)) );
        anim_enemy->setPosAt(0,QPointF(0,im->height()*(-1) ) );                                       // 0 и -10 тоже попробовать
        anim_enemy->setPosAt(0.5,QPointF(0,G_H/2 ));
        anim_enemy->setPosAt(1,QPointF(0,G_H+im->height()) );
        this->setPos( QPointF (0,im->height()*(-1)) );
    }
    else{
        this->setPos( QPointF( G_W-QPixmap("").width(),im->height()*(-1)));
        anim_enemy->setPosAt(0,QPointF(G_W-im->width(),im->height()*(-1)));                                       // 0 и -10 тоже попробовать
        anim_enemy->setPosAt(0.5,QPointF(G_W-im->width(),G_H/2 ));
        anim_enemy->setPosAt(1,QPointF(G_W-im->width(),G_H+im->height() ));
    }

    enemy_time_line->start();

    timer_id=startTimer(300);

    ammunition=2;                                                                           // запас выстрелов

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,100);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(enemy_time_line,SIGNAL(finished()),SLOT(deleteLater()));

}

void ENEMY_2::timerEvent(QTimerEvent *)
{

    if(ammunition){                                                                            // Если есть патроны
        if( (shuttle_pos.y()-100) > (this->pos().y()) ){    // Влупить со всех 4ёх стволов...
            emit Fire(QPoint(this->pos().x()+stvol_1.x(),this->pos().y()+stvol_1.y()),left_orientation );
            emit Fire(QPoint(this->pos().x()+stvol_2.x(),this->pos().y()+stvol_2.y()),left_orientation );
            emit Fire(QPoint(this->pos().x()+stvol_3.x(),this->pos().y()+stvol_3.y()),left_orientation );
            emit Fire(QPoint(this->pos().x()+stvol_4.x(),this->pos().y()+stvol_4.y()),left_orientation );
        }
        ammunition--;
    }

}

QPainterPath ENEMY_2::shape() const
{

    return *border;

}

QRectF ENEMY_2::boundingRect() const
{

    if(left_orientation)
        return QRectF( QPointF(0,0),im->size() );
    else
        return QRectF( QPointF(0,0),im->size() );


}

void ENEMY_2::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

ENEMY_2::~ENEMY_2()
{
}

int ENEMY_2::type() const
{

    return point;

}

void ENEMY_2::Damage_Detected(QPointF a, int d)
{

    a=QPointF();

    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(enemy_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            killTimer(timer_id);
            go->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }

}
